package com.farm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.farm.dao.InsertDao;
import com.farm.dao.InsertHomeDao;
import com.farm.service.InsertHomeService;
import com.farm.utils_all.HUtil;
import com.farm.utils_all.XVDateUtils;

@Service("InsertHomeService")
public class InsertHomeServiceimpl implements InsertHomeService{

	@Autowired
	private InsertHomeDao insertHomeDao;
	@Autowired
	private InsertDao insertDao;
	
	@Override
	public Map<String, Object> insert(Map<String, Object> map) {
		//u_id=174&u_rz=0&openid=o0z3K5GA82ndUO6I1qFYdDzUYH8E&tx=1&mobile=123
		//u_id 用户id   
		//u_rz 是否入住   0入住  1不入住
		//opening
		//tx 同行     0无  1有
		//mobile   同行电话
		
		//用户插入是否入住状态  
		int insertHome = insertHomeDao.insertHome(map);
		
		//查询人员信息  -- 查询签到用户信息
		Map<String, Object> selectHome = insertHomeDao.selectHome(map);
		
		//手机号查询同行人员用户信息
		Map<String, Object> selectPhone = insertHomeDao.selectPhone(HUtil.putInMap("u_phone",map.get("mobile")));
		
		System.out.println(selectHome);
		System.out.println(selectPhone);
		
		
		
		
		
		
		/**
		 * 用户 签到
		 * */
		 //修改签到信息和OPENID
	    insertDao.insertOpenid(HUtil.putInMap("u_qd",1,"openid",map.get("openid"),"u_id",selectHome.get("u_id")));
	    
	    //查询此人签到表信息
	    List<Map<String, Object>> selectff = insertDao.selectff(HUtil.putInMap("u_id",selectHome.get("u_id")));
	    if(selectff.size() == 0) {
	    	insertDao.insert(HUtil.putInMap("u_id",selectHome.get("u_id"),"f_id",selectHome.get("f_id"),"sign_in_time",XVDateUtils.getCurrenTimeMillis(),"u_name",selectHome.get("u_name")));
	    }
	    
	    
	    
	    /** 随行人员签到  */
	    if(selectPhone != null) {
		    
		    //查询此人签到表信息
		    List<Map<String, Object>> selectffaaa = insertDao.selectff(HUtil.putInMap("u_id",selectPhone.get("u_id")));
		    if(selectffaaa.size() == 0) {
		    	insertDao.insert(HUtil.putInMap("u_id",selectPhone.get("u_id"),"f_id",selectPhone.get("f_id"),"sign_in_time",XVDateUtils.getCurrenTimeMillis(),"u_name",selectPhone.get("u_name")));
		    }
	    }
	    
	    /***/
		
		
		
		
		
		
		//查询房间信息   --- 查询酒店状态正常的所有房间信息
		List<Map<String, Object>> selectHotelroom = insertHomeDao.selectHotelroom();
		
		
		System.out.println(selectHotelroom);
		//查询房间床位信息
		//List<Map<String, Object>> selectHotelbed = insertHomeDao.selectHotelbed();
		//System.out.println(selectHotelbed);
		
		
		//这是修改后的当前用户是否选择入住信息  u_rz   0入住  1不入住
		int o=Integer.valueOf(selectHome.get("u_rz").toString());
		//这是修改后的当前用户是否已经入住信息  u_home 0 已入住 1 未入住
		int e=Integer.valueOf(selectHome.get("u_home").toString());
		//这是是否有同行人员的标记    0无 1有 
		int tx=Integer.valueOf(map.get("tx").toString());
		
		//当前用户选择了入住
		if(o==0) {
			/**
			 * 判断当前用户是否已入住  已入住直接return 
			 * */
			List<Map<String, Object>> userBind = insertHomeDao.selectUserBind(HUtil.putInMap("u_id",selectHome.get("u_id")));
			if(userBind.size() > 0) {
				return HUtil.putInMap("id",insertHome,"msg","人员已分配,插入失败","code","400");
			}
			
			//存在同行人员  
			//if(tx==1) {
			
			//更换条件
			if(selectPhone != null) {
				/**
				 * 判断同行人员是否已入住  已入住直接return 
				 * */
				List<Map<String, Object>> sui_userBind = insertHomeDao.selectUserBind(HUtil.putInMap("u_id",selectPhone.get("u_id")));
				if(sui_userBind.size() > 0) {
					return HUtil.putInMap("id",insertHome,"msg","随行人员已分配,插入失败","code","400");
				}
				
				
				System.out.println("aaa");
				//遍历状态正常的所有酒店房间
				for(Map<String, Object> map2:selectHotelroom) {
					//当前酒店的装态    0未入住   1已入住   2已住满  
					int b=Integer.valueOf(map2.get("hr_status").toString());
					//当该酒店(未入住)并且(房间性别等于用户性别)并且(房间类型等于用户类型)
					if((b==0)&&(map2.get("hr_sex")==selectHome.get("u_sex"))&&(map2.get("hr_type")==selectHome.get("u_type"))) {
						
						//酒店房间id
						int d =Integer.valueOf(map2.get("hr_id").toString());
						//查询房间床位信息
						List<Map<String, Object>> selectHotelbed = insertHomeDao.selectHotelbed(HUtil.putInMap("hr_id",d));
						
						//该房间存在床位信息
						if(selectHotelbed != null) {
							System.out.println("bbb");
							//第一个床位的状态     0未分配   1已分配
							int a = Integer.valueOf(selectHotelbed.get(0).get("hb_status").toString());
							//酒店房间id 同上
							int c =Integer.valueOf(selectHotelbed.get(0).get("hr_id").toString());
							//int d =Integer.valueOf(map2.get("hr_id").toString());
							
							//遍历房间床位信息
							for(int i=0;i<selectHotelbed.size();i++) {
								System.out.println("ccc");
								//改变房间床位信息	--此操作会把该房间所有的床位修改成已入住状态
								insertHomeDao.insertHometel(HUtil.putInMap("hb_status",1,"hb_bed",selectHotelbed.get(i).get("hb_bed"),"hr_id",map2.get("hr_id")));
							}
							
							//改变人员房间信息		--修改当前用户的用户表信息
							int insertHomeUsers = insertHomeDao.insertHomeUsers(HUtil.putInMap("u_hotel",map2.get("hrn_name"),"u_eat_address",map2.get("hrn_eat_address"),"u_hoteltapy",map2.get("hrn_type"),"u_hotelnumber",map2.get("hr_room"),"u_hotelnumbertel",selectHotelbed.get(0).get("hb_bed"),"u_home",0,"u_id",selectHome.get("u_id"),"u_qd",1));
							//改变人员房间信息		--修改随行人员的用户表信息
							int insertHomeUsers2 = insertHomeDao.insertHomeUsers(HUtil.putInMap("u_hotel",map2.get("hrn_name"),"u_eat_address",map2.get("hrn_eat_address"),"u_hoteltapy",map2.get("hrn_type"),"u_hotelnumber",map2.get("hr_room"),"u_hotelnumbertel",selectHotelbed.get(1).get("hb_bed"),"u_home",0,"u_phone",map.get("mobile"),"u_qd",1));
							
							
							//如果两人的信息都修改成功
							if(insertHomeUsers>0&&insertHomeUsers2>0) {
								//插入签到信息    --实际为插入床位房间用户绑定信息
								int currenTimeMillis = XVDateUtils.getCurrenTimeMillis();
								 System.out.println(currenTimeMillis);
								 //插入当前用户的绑定信息
								int ss = insertHomeDao.insertHomeTime(HUtil.putInMap("hb_id",selectHotelbed.get(0).get("hb_id"),"hr_id",map2.get("hr_id"),"buh_addtime",currenTimeMillis,"u_id",selectHome.get("u_id")));
								 //插入随行人员的绑定信息
								int zz =  insertHomeDao.insertHomeTime(HUtil.putInMap("hb_id",selectHotelbed.get(1).get("hb_id"),"hr_id",map2.get("hr_id"),"buh_addtime",currenTimeMillis,"u_id",selectPhone.get("u_id")));
								 //改变房间信息		--此操作会把当前房间修改为满员的状态
								if(ss ==  1 && zz == 1) {
									insertHomeDao.insertHomeroom(HUtil.putInMap("hr_status",2,"hr_room",map2.get("hr_room")));
								} 
								 
								
								 return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息成功","code","200");
							}else {
								return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息失败","code","400");
							}
							
						}
						return HUtil.putInMap("id",insertHome,"msg","房间信息改变成功","code","201");
					}
				}
				
			}else {
		    //不存在同行人员
				System.out.println("aaa");
				
				//遍历状态正常的所有酒店房间
				for(Map<String, Object> map2:selectHotelroom) {
					//当前酒店的装态    0未入住   1已入住   2已住满  
					int b=Integer.valueOf(map2.get("hr_status").toString());
					//当该酒店(未入住)并且(房间性别等于用户性别)并且(房间类型等于用户类型)
					if((b==0)&&(map2.get("hr_sex")==selectHome.get("u_sex"))&&(map2.get("hr_type")==selectHome.get("u_type"))) {
						
						//该房间的id
					    int d =Integer.valueOf(map2.get("hr_id").toString());
					    
						//查询房间床位信息
						List<Map<String, Object>> selectHotelbed = insertHomeDao.selectHotelbed(HUtil.putInMap("hr_id",d));
						
						//遍历当前房间的所有床位信息
						for(Map<String, Object> map3:selectHotelbed) {
							System.out.println("bbb");
							//床位的状态     0未分配   1已分配
							int a = Integer.valueOf(map3.get("hb_status").toString());
							//房间id
							int c =Integer.valueOf(map3.get("hr_id").toString());
							
							//当房间相等(此条件可不写)并且床位状态为未入住的状态
							if((c==d)&&(a==0)) {
								System.out.println("ccc");
								
								//挪到成功里
								//改变房间信息		--修改房间的状态为已入住
								//insertHomeDao.insertHomeroom(HUtil.putInMap("hr_status",1,"hr_room",map2.get("hr_room")));
								//改变房间床位信息   --修改房间床位的状态为已分配
								int insertHometel = insertHomeDao.insertHometel(HUtil.putInMap("hb_status",1,"hb_bed",map3.get("hb_bed"),"hr_id",map2.get("hr_id")));
								//改变人员房间信息		--修改用户表的住宿信息
								int insertHomeUsers = insertHomeDao.insertHomeUsers(HUtil.putInMap("u_hotel",map2.get("hrn_name"),"u_eat_address",map2.get("hrn_eat_address"),"u_hoteltapy",map2.get("hrn_type"),"u_hotelnumber",map2.get("hr_room"),"u_hotelnumbertel",map3.get("hb_bed"),"u_home",0,"u_id",selectHome.get("u_id"),"u_qd",1));
								//修改住宿信息成功
								if(insertHomeUsers>0) {
									//插入签到信息      --实际为绑定房间床位用户信息
									int currenTimeMillis = XVDateUtils.getCurrenTimeMillis();
									 System.out.println(currenTimeMillis);
									 insertHomeDao.insertHomeTime(HUtil.putInMap("hb_id",map3.get("hb_id"),"hr_id",map2.get("hr_id"),"buh_addtime",currenTimeMillis,"u_id",selectHome.get("u_id")));
									
									 
									//挪到成功里
										//改变房间信息		--修改房间的状态为已入住
										insertHomeDao.insertHomeroom(HUtil.putInMap("hr_status",1,"hr_room",map2.get("hr_room")));
									 
									 return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息成功","code","200");
								}else {
									return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息失败","code","400");
								}
								
							}
						}
						return HUtil.putInMap("id",insertHome,"msg","房间信息改变成功","code","201");
					
					//当该酒店(已入住未住满时)并且(房间性别等于用户性别)并且(房间类型等于用户类型)并且(用户未入住)时
					}else if(b==1 && selectHome.get("u_sex")==map2.get("hr_sex") && selectHome.get("u_type")==map2.get("hr_type")&&e==1) {
						
						//当前房间id
						int d =Integer.valueOf(map2.get("hr_id").toString());
						//查询房间床位信息
						List<Map<String, Object>> selectHotelbed = insertHomeDao.selectHotelbed(HUtil.putInMap("hr_id",d));
						//遍历当前房间的所有床位
						for(Map<String, Object> map3:selectHotelbed) {
							//床位的状态     0未分配   1已分配
							int a = Integer.valueOf(map3.get("hb_status").toString());
							//房间id
							int c =Integer.valueOf(map3.get("hr_id").toString());
							//当房间相等(此条件可不写)并且床位状态为未入住的状态
							if(c==d&&a==0) {
								
								//挪到成功里
								
								//改变房间信息   	--修改房间的状态为已入住
								//insertHomeDao.insertHomeroom(HUtil.putInMap("hr_status",2,"hr_room",map2.get("hr_room")));
								//改变房间床位信息     --修改房间床位的状态为已分配
								insertHomeDao.insertHometel(HUtil.putInMap("hb_status",1,"hb_bed",map3.get("hb_bed"),"hr_id",map2.get("hr_id")));
								//改变人员房间信息  	--修改用户表的住宿信息
								int insertHomeUsers = insertHomeDao.insertHomeUsers(HUtil.putInMap("u_hotel",map2.get("hrn_name"),"u_eat_address",map2.get("hrn_eat_address"),"u_hoteltapy",map2.get("hrn_type"),"u_hotelnumber",map2.get("hr_room"),"u_hotelnumbertel",map3.get("hb_bed"),"u_home",0,"u_id",selectHome.get("u_id"),"u_qd",1));
								//修改住宿信息成功
								if(insertHomeUsers>0) {
									//插入房间使用时间    --实际为绑定房间床位用户信息
									 int currenTimeMillis = XVDateUtils.getCurrenTimeMillis();
									 System.out.println(currenTimeMillis);
									 insertHomeDao.insertHomeTime(HUtil.putInMap("hb_id",map3.get("hb_id"),"hr_id",map2.get("hr_id"),"buh_addtime",currenTimeMillis,"u_id",selectHome.get("u_id")));
									
									//挪到成功里
										
										//改变房间信息   	--修改房间的状态为已入住
										insertHomeDao.insertHomeroom(HUtil.putInMap("hr_status",2,"hr_room",map2.get("hr_room")));
									 
									 return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息成功","code","202");
								}else {
									return HUtil.putInMap("id",insertHome,"msg","插入人员房间信息失败","code","401");
								}
								
							}
						}
						return HUtil.putInMap("id",insertHome,"msg","房间信息改变成功","code","203");
					}
				}
			}
			
			
		}
		return HUtil.putInMap("id",insertHome,"msg","房间信息改变成功","code","200");
	}
	
	
@Override
public List<Map<String, Object>> selectUserBind(Map<String, Object> map) {
	return insertHomeDao.selectUserBind(map);
}
}
